package com.log4jviewer.ui.preferences.additional;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;

import org.eclipse.jface.preference.IPreferenceStore;
import org.junit.Test;

public class ColumnModelListTest {

    @Test
    public void columnPrefsTest() {
        IPreferenceStore storeMock = mock(IPreferenceStore.class);
        when(storeMock.getString("level_column")).thenReturn(LogFieldColumn.LEVEL_COLUMN.getDefaultPrefs()).thenReturn(
                "3:100:false");
        when(storeMock.getString("category_column")).thenReturn(LogFieldColumn.CATEGORY_COLUMN.getDefaultPrefs())
                .thenReturn("0:100:true");
        when(storeMock.getString("message_column")).thenReturn(LogFieldColumn.MESSAGE_COLUMN.getDefaultPrefs())
                .thenReturn("6:100:true");
        when(storeMock.getString("line_column")).thenReturn(LogFieldColumn.LINE_COLUMN.getDefaultPrefs()).thenReturn(
                "4:100:false");
        when(storeMock.getString("date_column")).thenReturn(LogFieldColumn.DATE_COLUMN.getDefaultPrefs()).thenReturn(
                "2:100:false");
        when(storeMock.getString("ndc_column")).thenReturn(LogFieldColumn.NDC_COLUMN.getDefaultPrefs()).thenReturn(
                "1:100:true");
        when(storeMock.getString("throwable_column")).thenReturn(LogFieldColumn.THROWABLE_COLUMN.getDefaultPrefs())
                .thenReturn("5:100:false");

        ColumnModelList columnModelList = new ColumnModelList(storeMock);
        ColumnModel[] columnItems = columnModelList.getColumnItems();

        Assert.assertEquals(7, columnItems.length);

        Assert.assertEquals(0, columnItems[0].getColumnIndex());
        Assert.assertEquals(100, columnItems[0].getColumnWidth());
        Assert.assertEquals(true, columnItems[0].isDisplay());

        Assert.assertEquals(1, columnItems[1].getColumnIndex());
        Assert.assertEquals(100, columnItems[1].getColumnWidth());
        Assert.assertEquals(true, columnItems[1].isDisplay());

        Assert.assertEquals(2, columnItems[2].getColumnIndex());
        Assert.assertEquals(100, columnItems[2].getColumnWidth());
        Assert.assertEquals(true, columnItems[2].isDisplay());

        Assert.assertEquals(3, columnItems[3].getColumnIndex());
        Assert.assertEquals(100, columnItems[3].getColumnWidth());
        Assert.assertEquals(true, columnItems[3].isDisplay());

        Assert.assertEquals(4, columnItems[4].getColumnIndex());
        Assert.assertEquals(100, columnItems[4].getColumnWidth());
        Assert.assertEquals(true, columnItems[4].isDisplay());

        Assert.assertEquals(5, columnItems[5].getColumnIndex());
        Assert.assertEquals(100, columnItems[5].getColumnWidth());
        Assert.assertEquals(true, columnItems[5].isDisplay());

        Assert.assertEquals(6, columnItems[6].getColumnIndex());
        Assert.assertEquals(100, columnItems[6].getColumnWidth());
        Assert.assertEquals(true, columnItems[6].isDisplay());

        columnModelList = new ColumnModelList(storeMock);
        columnItems = columnModelList.getColumnItems();

        Assert.assertEquals(3, columnItems[0].getColumnIndex());
        Assert.assertEquals(100, columnItems[0].getColumnWidth());
        Assert.assertEquals(false, columnItems[0].isDisplay());

        Assert.assertEquals(0, columnItems[1].getColumnIndex());
        Assert.assertEquals(100, columnItems[1].getColumnWidth());
        Assert.assertEquals(true, columnItems[1].isDisplay());

        Assert.assertEquals(6, columnItems[2].getColumnIndex());
        Assert.assertEquals(100, columnItems[2].getColumnWidth());
        Assert.assertEquals(true, columnItems[2].isDisplay());

        Assert.assertEquals(4, columnItems[3].getColumnIndex());
        Assert.assertEquals(100, columnItems[3].getColumnWidth());
        Assert.assertEquals(false, columnItems[3].isDisplay());

        Assert.assertEquals(2, columnItems[4].getColumnIndex());
        Assert.assertEquals(100, columnItems[4].getColumnWidth());
        Assert.assertEquals(false, columnItems[4].isDisplay());

        Assert.assertEquals(1, columnItems[5].getColumnIndex());
        Assert.assertEquals(100, columnItems[5].getColumnWidth());
        Assert.assertEquals(true, columnItems[5].isDisplay());

        Assert.assertEquals(5, columnItems[6].getColumnIndex());
        Assert.assertEquals(100, columnItems[6].getColumnWidth());
        Assert.assertEquals(false, columnItems[6].isDisplay());

        columnModelList.moveUp(2);
        columnModelList.moveDown(5);

        Assert.assertEquals(3, columnItems[0].getColumnIndex());
        Assert.assertEquals(100, columnItems[0].getColumnWidth());
        Assert.assertEquals(false, columnItems[0].isDisplay());

        Assert.assertEquals(6, columnItems[1].getColumnIndex());
        Assert.assertEquals(100, columnItems[1].getColumnWidth());
        Assert.assertEquals(true, columnItems[1].isDisplay());

        Assert.assertEquals(0, columnItems[2].getColumnIndex());
        Assert.assertEquals(100, columnItems[2].getColumnWidth());
        Assert.assertEquals(true, columnItems[2].isDisplay());

        Assert.assertEquals(4, columnItems[3].getColumnIndex());
        Assert.assertEquals(100, columnItems[3].getColumnWidth());
        Assert.assertEquals(false, columnItems[3].isDisplay());

        Assert.assertEquals(2, columnItems[4].getColumnIndex());
        Assert.assertEquals(100, columnItems[4].getColumnWidth());
        Assert.assertEquals(false, columnItems[4].isDisplay());

        Assert.assertEquals(5, columnItems[5].getColumnIndex());
        Assert.assertEquals(100, columnItems[5].getColumnWidth());
        Assert.assertEquals(false, columnItems[5].isDisplay());

        Assert.assertEquals(1, columnItems[6].getColumnIndex());
        Assert.assertEquals(100, columnItems[6].getColumnWidth());
        Assert.assertEquals(true, columnItems[6].isDisplay());
    }
}
